#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int val;
    struct ListNode *next;
};

struct ListNode* swapPairs(struct ListNode* head) {
    struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
    dummy->next = head;
    struct ListNode* prev = dummy;

    while (head && head->next) {
        struct ListNode* first_node = head;
        struct ListNode* second_node = head->next;

        prev->next = second_node;
        first_node->next = second_node->next;
        second_node->next = first_node;

        prev = first_node;
        head = first_node->next;
    }

    return dummy->next;
}

int main() {
    struct ListNode* node1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* node2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* node3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* node4 = (struct ListNode*)malloc(sizeof(struct ListNode));

    node1->val = 1;
    node2->val = 2;
    node3->val = 3;
    node4->val = 4;

    node1->next = node2;
    node2->next = node3;
    node3->next = node4;
    node4->next = NULL;

    struct ListNode* swappedList = swapPairs(node1);

    while (swappedList) {
        printf("%d ", swappedList->val);
        swappedList = swappedList->next;
    }

    return 0;
}